import java.util.HashMap;

/**
 * @program: LeetCode
 * @ClassName Num035_copyRandomList
 * @description: LeetCode : 剑指 Offer 35. 复杂链表的复制
 * @author: WXY
 * @create: 2022-12-09 16:36
 * @Version 1.0
 **/


public class Num035_copyRandomList {
    static class Node {
        int val;
        Node next;
        Node random;

        public Node(int val) {
            this.val = val;
            this.next = null;
            this.random = null;
        }
    }
    public static Node copyRandomList(Node head) {
        HashMap<Node, Node> map = new HashMap<>();
        Node cur = head;
        while (cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        cur = head;
        while (cur != null) {
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}
